// Use WinExec( ..., 9 ) to execute the _same_ external application
// using the previous one if already running.

#include <WinTen.h>
#include <Windows.h>
#include <extend.h>

extern HANDLE _hInstance;

BOOL SysRefresh( void );

//----------------------------------------------------------------------------//

CLIPPER WINEXEC()
{
   WORD wMode = ( PCOUNT() > 1 ) ? _parni( 2 ): SW_NORMAL;

   _retni( WinExec( _parc( 1 ), wMode ) );
}

//----------------------------------------------------------------------------//

CLIPPER GETWINDIR() //  --> cWindowsPath
{
   BYTE bBuffer[ 255 ];

   GetWindowsDirectory( bBuffer, 254 );
   _retc( bBuffer );
}

//----------------------------------------------------------------------------//

CLIPPER GETSYSDIR() // --> cWindowsSystemPath
{
   BYTE bBuffer[ 255 ];

   GetSystemDirectory( bBuffer, 254 );
   _retc( bBuffer );
}

//----------------------------------------------------------------------------//

// CLIPPER GETNUMTASK() // S()  --> nTasksInUse
// {
//   _retni( GetNumTasks() );
// }

//----------------------------------------------------------------------------//

CLIPPER GETVERSION() //  --> anVersions
{
   LONG lInfo = GetVersion();

   _reta( 4 );
   _storni( LOBYTE( LOWORD( lInfo ) ), -1, 1 );
   _storni( HIBYTE( LOWORD( lInfo ) ), -1, 2 );
   _storni( HIBYTE( HIWORD( lInfo ) ), -1, 3 );
   _storni( LOBYTE( HIWORD( lInfo ) ), -1, 4 );
}

//----------------------------------------------------------------------------//

CLIPPER GETMODULEF() // ILENAME()  hInstance --> cAppName
{
   BYTE bBuffer[ 250 ];

   GetModuleFileName( ( HANDLE ) _parnl( 1 ), bBuffer, 249 );
   _retc( bBuffer );
}

//----------------------------------------------------------------------------//

CLIPPER WAITRUN()
{
   UINT hInst = WinExec( _parc( 1 ),
                         IF( ( PCOUNT() > 1 ), _parnl( 2 ), SW_NORMAL ) );
//   WORD wCount   = GetModuleUsage( hInst );

//   while( SysRefresh() && GetModuleUsage( hInst ) > ( wCount - 1 ) );

   _retnl( ( LONG ) hInst );
}

//----------------------------------------------------------------------------//

// CLIPPER EXITWINDOW() // SEXEC( cExeName, cParams ) --> lSuccess
// {
//   _retl( ExitWindowsExec( _parc( 1 ), _parc( 2 ) ) );
// }

//----------------------------------------------------------------------------//

CLIPPER EXITWND() //( nRetCode ) --> lSuccess
{
   _retl( ExitWindows( _parnl( 1 ), 0 ) );
}

//----------------------------------------------------------------------------//
/*

CLIPPER SETMULTIPL() // E( lOnOff ) Permits multiple same FW apps !!!
{
   HMODULE hModuleSel = 0;
   LPSTR moduleName, fileName;
   BYTE bLen;
   BOOL bOn = _parl( 1 );

   if( ! ( hModuleSel = SELECTOROF( GlobalLock( GetModuleHandle(
                        MAKELP( 0, _hInstance ) ) ) ) ) )
      return;

   moduleName = MAKELP( hModuleSel, * ( WORD FAR * ) MAKELP(
                        hModuleSel, 0x26 ) );
   fileName   = MAKELP( hModuleSel, * ( WORD FAR * ) MAKELP(
                        hModuleSel, 0x0A ) );

   bLen = * moduleName++;

   while( bLen )
   {
      if( bOn )
      {
         if( ( * moduleName >= 'A' ) && ( * moduleName <= 'Z' ) )
            * moduleName += ( 'a' - 'A' );
         bLen--;
         moduleName++;
      }
      else
      {
         if( ( * moduleName >= 'a' ) && ( * moduleName <= 'z' ) )
            * moduleName -= ( 'a' - 'A' );
         bLen--;
         moduleName++;
      }
   }

   fileName += *fileName - 1; // position at the end of the filename
   while( TRUE )
   {
      // Stop when we come to a directory separator or colon
      if( ( * fileName == '\\' ) || ( * fileName == '/' ) ||
          ( * fileName == ':' ) )
      {
         GlobalUnlock( GetModuleHandle( MAKELP( 0, _hInstance ) ) );
         return;
      }

      if( bOn )
      {
         if( ( * fileName >= 'A' ) && ( * fileName <= 'Z' ) )
            * fileName += ( 'a' - 'A' );
      }
      else
      {
         if( ( * fileName >= 'a' ) && ( * fileName <= 'z' ) )
            * fileName -= ( 'a' - 'A' );
      }
      fileName--;
   }
}
*/

//----------------------------------------------------------------------------//
